import { ref, computed } from "vue";

export const useStack = () => {
  const stack = ref<string[]>([]);

  const isTop = computed(() => {
    return stack.value.length === 1;
  });

  const getCurrentCode = (): string => {
    return stack.value.at(-1);
  };

  const push = (code): void => {
    stack.value.push(code);
  };

  const pop = (): void => {
    stack.value.pop();
  };

  const reset = () => {
    while (stack.value.length > 1) {
      pop();
    }
  };

  return {
    getCurrentCode,
    push,
    pop,
    reset,
    isTop
  };
};
